R Matrix Exercises

Through these exercises we will review the matrix data structure and perhaps introduce you to a few ideas for you to discover on your own! Just answer the questions below written in bold:

Ex 1: Create 2 vectors A and B, where A is (1,2,3) and B is (4,5,6). With these vectors, use the cbind() or rbind() function to create a 2 by 3 matrix from the vectors. You'll need to figure out which of these binding functions is the correct choice.

In [5]:
Out[5]:
A123
B456

Ex 2: Create a 3 by 3 matrix consisting of the numbers 1-9. Create this matrix using the shortcut 1:9 and by specifying the nrow argument in the matrix() function call. Assign this matrix to the variable mat

In [6]:

Ex 3: Confirm that mat is a matrix using is.matrix()

In [7]:
Out[7]:
TRUE

Ex 4: Create a 5 by 5 matrix consisting of the numbers 1-25 and assign it to the variable mat2. The top row should be the numbers 1-5.

In [11]:
Out[11]:
12345
6 7 8 910
1112131415
1617181920
2122232425

Ex 5: Using indexing notation, grab a sub-section of mat2 from the previous exercise that looks like this:

[7,8]
[12,13]
In [12]:
Out[12]:
78
1213

Ex 6: Using indexing notation, grab a sub-section of mat2 from the previous exercise that looks like this:

[19,20]
[24,25]
In [17]:
Out[17]:
1920
2425

Ex 7: What is the sum of all the elements in mat2?

In [18]:
Out[18]:
325

Ex 8: Ok time for our last exercise! Find out how to use runif() to create a 4 by 5 matrix consisting of 20 random numbers (4*5=20).

In [25]:
Out[25]:
21.0399641.2168951.4671635.2445165.69687
60.81269681.32660681.54530094.459102 3.403767
72.1324132.4819418.7460035.1630390.70033
1.79834535.55823394.30099117.99132011.074018
In [1]:
#help(runif)

Great Job!